//
//  YSPMainTabBarController.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/15.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //也可以统一设置tabBar的颜色
//        self.tabBar.tintColor = UIColor.orangeColor()
        
        //设置自定义的tabbar
        let mainTabBar = YSPMainTabBar()
        setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.composeClosure = { () -> Void in
            if YSPUserAccount.userLogin {
                //如果用户登录了，弹出撰写控制器
                let composeVC = YSPComposeController()
                self.presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
            } else {
                //如果没登录，就弹出授权界面
                let oauthVC = YSPOauthController()
                self.presentViewController(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
            }
            
        }
        
        let homeVC = YSPHomeController()
        self.addChildVC(homeVC, title: "首页", imageName: "tabbar_home")
        
        let discoverVC = YSPDiscoverController()
        self.addChildVC(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        let messageVC = YSPMessageController()
        self.addChildVC(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        let profileVC = YSPProfileController()
        self.addChildVC(profileVC, title: "我", imageName: "tabbar_profile")
        
        

        }

    private func addChildVC(controller: UIViewController, title: String, imageName: String) {
        //包装导航控制器
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
        //设置title，和tabBar下面的那个字
        controller.title = title
        
        //设置图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //设置高亮图片,设置图片渲染出来的颜色
        //也可以统一在控制器里设置tintColor
        let highLightedName = imageName + "_highlighted"
        controller.tabBarItem.selectedImage = UIImage(named: highLightedName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置文字颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
    }
}
